package com.ssi.myshop.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.ssi.myshop.R;
import com.ssi.myshop.bean.CategoryContentBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CategoryRightAdapter extends RecyclerView.Adapter<CategoryRightAdapter.InnerHolder> {
    private List<CategoryContentBean> mData = new ArrayList<>();
    private OnRightItemClickListener mListener = null;

    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.category_right_item, parent, false);
        return new InnerHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, int position) {
        CategoryContentBean bean = mData.get(position);
        holder.setContentData(bean);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onRightItemClick(bean);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public class InnerHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.right_top_img)
        ImageView mImageView;

        @BindView(R.id.right_bottom_tv)
        TextView mTextView;

        public InnerHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void setContentData(CategoryContentBean bean) {
            Glide.with(itemView.getContext()).load(bean.getPic()).into(mImageView);
            mTextView.setText(bean.getName());
        }
    }

    public void setData(List<CategoryContentBean> data) {
        if (data != null && data.size() > 0) {
            mData.clear();
            mData.addAll(data);
            notifyDataSetChanged();
        } else {
            mData.clear();
            notifyDataSetChanged();
        }
    }

    public void setOnRightItenClick(OnRightItemClickListener listener) {
        this.mListener = listener;
    }

    public interface OnRightItemClickListener {
        void onRightItemClick(CategoryContentBean bean);
    }

}
